/*
Copyright © 2024 wjsmc
*/
package internal

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"nodemessage.com/ruov-cli/builder"
	"nodemessage.com/ruov-cli/rlog"
	"nodemessage.com/ruov-cli/types"
	"nodemessage.com/ruov-cli/utils"
)

var (
	RuovConfPath    = ".ruov"
	RuovXmlFullPath = filepath.Join(RuovConfPath, "ruov.xml")
	RuovApiPath     = "admin-api"
	RuovFrontPath   = "admin-front"
)

var (
	genController  = false
	genService     = false
	genServiceImpl = false
	genMapper      = false
	genMapperXml   = false
	genPo          = false
	genVo          = false
	genDto         = false
)

var logicDir = "logic"

var PomHeader = map[string]string{
	"xmlns":              "http://maven.apache.org/POM/4.0.0",
	"xmlns:xsi":          "http://www.w3.org/2001/XMLSchema-instance",
	"xsi:schemaLocation": "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
}

var banner = `
 ______     __  __     ______     __   __   
/\  == \   /\ \/\ \   /\  __ \   /\ \ / /   
\ \  __<   \ \ \_\ \  \ \ \/\ \  \ \ \'/    
 \ \_\ \_\  \ \_____\  \ \_____\  \ \__|    
  \/_/ /_/   \/_____/   \/_____/   \/_/    `

func printBanner() {
	color.New(color.FgCyan, color.Bold).Println(banner)
	color.New(color.FgGreen).Println("Welcome to ruov - Your Command Line Tool. ")
	color.New(color.FgHiRed).Println("                                  --- " + builder.VERSION)
	fmt.Println()
}

func printStatement() {
	fmt.Println()
	color.New(color.FgGreen).Println("Please focus: https://gitee.com/wangjusanmu/ruov")
}

func setterGenAll() {
	genController = true
	genService = true
	genServiceImpl = true
	genMapper = true
	genMapperXml = true
	genPo = true
	genVo = true
	genDto = true
}

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
	Use:   "ruov",
	Short: "基于ruoyi-vue3的cli工具",
	Long:  color.New(color.FgYellow).Sprintf(`基于ruoyi-vue3的cli工具,可快速构建系统.提供了庞大内置命令,为开发者提供更加方便快捷的使用体验.`),
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		printBanner()
	},
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		printStatement()
	},
}

func init() {
	RootCmd.AddCommand(initCmd)
	RootCmd.AddCommand(runCmd)
	RootCmd.AddCommand(genCmd)
	RootCmd.AddCommand(installCmd)
	RootCmd.AddCommand(useCmd)
	RootCmd.AddCommand(gitCmd)
	RootCmd.AddCommand(globalCmd)
}

func checkIsApiPath() types.Config {
	currentDir, err := os.Getwd()
	if err != nil {
		rlog.Logger.Fatal("获取当前目录失败:%v", err)
	}
	dirName := filepath.Base(currentDir)
	if dirName != RuovApiPath {
		rlog.Logger.Fatal("请在admin-api目录下执行此命令")
	}
	ruovPath := filepath.Dir(currentDir)
	ruovXmlPath := filepath.Join(ruovPath, RuovXmlFullPath)
	if !utils.FilePathExists(ruovXmlPath) {
		rlog.Logger.Fatal("请确保在脚手架生成项目中执行此命令")
	}
	if !utils.FilePathExists(logicDir) {
		rlog.Logger.Fatal("请确保在脚手架生成项目中执行此命令,当前未发现 [logic] module.")
	}
	config, err := utils.XmlFileReadXMLConfig[types.Config](ruovXmlPath)
	if err != nil {
		rlog.Logger.Fatal("读取项目配置文件失败:%v", err)
	}
	rlog.Logger.Info("目录文件校验通过,模块生成中...")
	return *config
}

func readLogicPom() *types.TplLoginPom {
	config, err := utils.XmlFileReadXMLConfig[types.TplLoginPom](filepath.Join(logicDir, "pom.xml"))
	if err != nil {
		rlog.Logger.Fatal("读取logic模块pom文件失败:%v", err)
	}
	return config
}
